<template>
  <div class="game-list-page"> </div>
</template>

<script>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { gameAPI } from '../api'

export default {
  name: 'GameList',
  setup() {
    const router = useRouter()
    const games = ref([])
    const searchQuery = ref('')
    const selectedCategory = ref('全部')
    const loading = ref(false)

    const categories = ['全部', 'RPG', 'MOBA', 'FPS', 'Sandbox', 'Strategy', 'Adventure']

    const filteredGames = computed(() => {
      let filtered = games.value

      // 分类筛选
      if (selectedCategory.value !== '全部') {
        filtered = filtered.filter(game => game.category === selectedCategory.value)
      }

      // 搜索筛选
      if (searchQuery.value) {
        filtered = filtered.filter(game =>
          game.title.toLowerCase().includes(searchQuery.value.toLowerCase()) ||
          game.developer.toLowerCase().includes(searchQuery.value.toLowerCase())
        )
      }

      return filtered
    })

    const loadGames = async () => {
      try {
        loading.value = true
        const response = await gameAPI.getGames()
        if (response.code === 200) {
          // 为每个游戏加载评论数
          const gamesWithCommentCount = await Promise.all(
            response.data.map(async (game) => {
              try {
                const commentResponse = await commentAPI.getComments(game.id)
                return {
                  ...game,
                  commentCount: commentResponse.code === 200 ? commentResponse.data.length : 0
                }
              } catch (error) {
                return { ...game, commentCount: 0 }
              }
            })
          )
          games.value = gamesWithCommentCount
        }
      } catch (error) {
        console.error('加载游戏失败:', error)
        ElMessage.error('加载游戏失败')
      } finally {
        loading.value = false
      }
    }

    const filterByCategory = (category) => {
      selectedCategory.value = category
    }

    const handleSearch = () => {
      // 搜索逻辑已在计算属性中处理
    }

    const viewGameDetail = (gameId) => {
      router.push(`/games/${gameId}`)
    }

    const likeGame = async (gameId) => {
      try {
        const response = await gameAPI.likeGame(gameId)
        if (response.code === 200) {
          ElMessage.success('点赞成功')
          // 更新本地数据
          const game = games.value.find(g => g.id === gameId)
          if (game) {
            game.likeCount += 1
          }
        }
      } catch (error) {
        console.error('点赞失败:', error)
        ElMessage.error('点赞失败')
      }
    }

    const favoriteGame = async (gameId) => {
      try {
        const response = await gameAPI.favoriteGame(gameId)
        if (response.code === 200) {
          ElMessage.success('收藏成功')
          // 更新本地数据
          const game = games.value.find(g => g.id === gameId)
          if (game) {
            game.favoriteCount += 1
          }
        }
      } catch (error) {
        console.error('收藏失败:', error)
        ElMessage.error('收藏失败')
      }
    }

    const formatDate = (date) => {
      if (!date) return '未知'
      return new Date(date).toLocaleDateString('zh-CN')
    }

    onMounted(() => {
      loadGames()
    })

    return {
      games,
      filteredGames,
      searchQuery,
      selectedCategory,
      categories,
      loading,
      filterByCategory,
      handleSearch,
      viewGameDetail,
      likeGame,
      favoriteGame,
      formatDate
    }
  }
}
</script>

<style scoped>
.game-list-page {
  min-height: 100vh;
  background: #f5f5f5;
}

/* 导航栏样式 */
.navbar {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 1rem 0;
}

.nav-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo {
  text-decoration: none;
}

.logo h1 {
  color: white;
  font-size: 1.8rem;
  font-weight: bold;
}

.nav-menu {
  display: flex;
  gap: 2rem;
}

.nav-item {
  color: white;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.nav-item:hover,
.nav-item.active {
  color: #f0f0f0;
}

/* 搜索和筛选栏 */
.search-filter-bar {
  background: white;
  padding: 2rem;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  margin: 2rem 0;
}

.search-box {
  margin-bottom: 1.5rem;
}

.filter-tabs {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.filter-btn {
  padding: 0.5rem 1rem;
  border: 2px solid #e0e0e0;
  background: white;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: 500;
}

.filter-btn:hover {
  border-color: #667eea;
  color: #667eea;
}

.filter-btn.active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-color: #667eea;
  color: white;
}

/* 游戏网格 */
.games-container {
  margin-bottom: 2rem;
}

.games-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 2rem;
}

.game-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  cursor: pointer;
}

.game-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.15);
}

.game-image {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.game-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.game-card:hover .game-image img {
  transform: scale(1.1);
}

.game-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.game-card:hover .game-overlay {
  opacity: 1;
}

.game-rating {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: rgba(255, 255, 255, 0.9);
  color: #333;
  padding: 0.3rem 0.6rem;
  border-radius: 15px;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  font-weight: bold;
}

.game-actions {
  display: flex;
  gap: 1rem;
}

.action-btn {
  background: rgba(255, 255, 255, 0.9);
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  transition: all 0.3s ease;
  font-weight: 500;
}

.action-btn:hover {
  background: white;
  transform: translateY(-2px);
}

.like-btn:hover {
  color: #f56c6c;
}

.favorite-btn:hover {
  color: #e6a23c;
}

.game-info {
  padding: 1.5rem;
}

.game-info h3 {
  font-size: 1.3rem;
  margin-bottom: 0.5rem;
  color: #333;
  font-weight: bold;
}

.game-developer {
  color: #666;
  margin-bottom: 0.3rem;
  font-size: 0.9rem;
}

.game-category {
  color: #667eea;
  font-weight: 500;
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

.game-stats {
  display: flex;
  gap: 1rem;
  align-items: center;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  color: #666;
  font-size: 0.8rem;
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 4rem 2rem;
  color: #666;
}

.empty-icon {
  font-size: 4rem;
  margin-bottom: 1rem;
}

.empty-state h3 {
  font-size: 1.5rem;
  margin-bottom: 0.5rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-content {
    flex-direction: column;
    gap: 1rem;
  }

  .search-filter-bar {
    margin: 1rem 0;
    padding: 1rem;
  }

  .games-grid {
    grid-template-columns: 1fr;
    gap: 1rem;
  }

  .filter-tabs {
    justify-content: center;
  }
}
</style>